<template>
  <div class="index">
    <div class="formBox">
        <p class="item"><span class="name">类型名称 ：</span><input v-model="date.category_name" class="slong" placeholder="请输入内容"></input></p>
        <p class="item"><span class="name">分类父级 ：</span>
            <select v-model="date.parent_id">
              <option value ="0">无</option>
              <option v-for="(item,index) in list" :value="item.id">{{item.category_name}}</option>
            </select>
        </p>
        <p class="item inputBox"><button @click="submit" class="submitInput">确定</button><button @click="empty" class="emptyInput">重置</button></p>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios'
import { Get, Post ,Put ,Delete} from '@/config/api'

export default {
  name: 'index',
  data() {
    return {
        list:[],
        date:{
          category_name:'',
          parent_id:0
        }
    }
  },
  mounted(){
    this.getData()
  },
  methods: {
    //获取商品分类
    async getData() {
      if(this.Check()){
        let reg=await Get('/goods/category?parent_id=0',this.$store.state.token)
        console.log(reg)
        this.list=reg.data
      }
    },
    //提交
    async submit() {
      if(this.Check()){
        let data;
        if(this.date.parent_id==0){
          data={
              "category_name":this.date.category_name
          }
        }else{
          data={
              "category_name":this.date.category_name,
              "parent_id":this.date.parent_id
          }
        }
        let reg=await Post('/goods/category',this.$store.state.token,data)
        console.log(reg)
        alert('添加成功')
        this.$router.push('/classList')
      }

    },
    //清空
    empty() {

    }
  }
}

</script>